package cn.lkx.eduservice.controller.front;

import cn.lkx.commonutils.R;
import cn.lkx.eduservice.entity.EduCourse;
import cn.lkx.eduservice.entity.EduTeacher;
import cn.lkx.eduservice.service.EduCourseService;
import cn.lkx.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台讲师 控制器 讲师分页 详情
 */
@RestController
@RequestMapping("/eduservice/frontteacher")
@CrossOrigin
public class TeacherFrontController {
    @Autowired
    private EduTeacherService teacherService;
    @Autowired
    private EduCourseService courseService;

    //讲师列表分页查询
    @GetMapping("pageFind/{page}/{limit}")
    public R pageFind(@PathVariable Long page,@PathVariable Long limit){
        Page<EduTeacher> pageParam = new Page<>(page,limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        teacherService.page(pageParam,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageParam.getTotal());
        map.put("items",pageParam.getRecords());
        map.put("pages",pageParam.getPages());
        map.put("current",pageParam.getCurrent());
        map.put("hasNext",pageParam.hasNext());  //是否存在下一页
        map.put("hasPrevious",pageParam.hasPrevious());  //是存在上一页
        return R.ok().data(map);
    }

    @GetMapping("getTeacherDetail/{id}")
    public R getTeacherDetailById(@PathVariable String id){
        //1、查询讲师信息
        EduTeacher teacher = teacherService.getById(id);
        //2、根据讲师id查询课程
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",id).orderByDesc("gmt_create");
        List<EduCourse> courseList = courseService.list(queryWrapper);
        return R.ok().data("teacher",teacher).data("courseList",courseList);
    }

}
